var express = require('express');
var mongoose = require('mongoose');

var router = express.Router();

var commentSchema = new mongoose.Schema({
  articleId: String,
  commentVal: String,
  createTime: {
    type: Date,
    default: Date.now
  }
});
var CommentModel = mongoose.model('CommentModel', commentSchema);
// GET list
router.get('/get', (req, res, next) => {
  var articleId = req.query.articleId;
  CommentModel.count({articleId}, (err, total) => {
    CommentModel.find({articleId}, (err, data) => {
      if (err) return next(err);
      return res.json({
        success: true,
        msg: '获取成功',
        dataList: data,
        total: total
      })
    })
  })
})

// POST add
router.post('/add', (req, res, next) => {
  var comment = new CommentModel(req.body);
  comment.save((err) => {
    if (err) return next(err);
    if(err) return next(err);
    return res.json({
      success: true,
      msg: '评价成功'
    })
  })
})

module.exports = router;

